package com.lei.study.cds.unit5;

import com.lei.study.cds.unit2.MvelCalculateTools;
import org.mvel2.MVEL;
import org.mvel2.ParserContext;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 伍磊
 */
public class Add {

    public static void main(String[] args) {
        ParserContext parserContext = MvelCalculateTools.initParse();

        String importPackage = "import java.lang.Math;  import java.math.BigDecimal;";


        String content = "def defMin(a, b) {return new BigDecimal(Math.min(a, b) + '');}";


        String express = importPackage + content + "defMin(x, y) + AVG(1, 10, 5, 0)";

        Map<String, Object> map = new HashMap<>();
        map.put("x", 1);
        map.put("y", 0.1);

        Serializable compiled = MVEL.compileExpression(express, parserContext);
        System.out.println(MVEL.executeExpression(compiled, map));
    }
}
